package com.study.services;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.jdao.ICategoryDAO;
import com.study.model.jdao.IDictionaryDAO;
import com.study.model.jdao.IGoodDAO;
import com.study.model.mdao.GoodDAO;
import com.study.model.pojo.Good;

@Service
@Transactional(rollbackOn = Exception.class)
public class GoodsService {
	@Autowired
	GoodDAO mapper;
	@Autowired
	IGoodDAO dao;
	@Autowired
	ICategoryDAO categoryDAO;
	@Autowired
	IDictionaryDAO disDao;
	
	/**
	 * 更多查询
	 * @param pageNum
	 * @param pageSize
	 * @param gname
	 * @param catname
	 * @param gid
	 * @param status
	 * @param xprice
	 * @param dprice
	 * @return 
	 */
	public List<JSONObject> more(int pageNum, int pageSize,String gname,String catname,String gid,

					String status,String xprice,String dprice,String xnum,String dnum) {
		PageHelper.startPage(pageNum, pageSize);
		return mapper.selectMore(gname,catname, gid, status, xprice, dprice,xnum,dnum);
	}
	
	/**
	 * 查询所有
	 * @return
	 */
	public List<JSONObject> queryAll() {
		return mapper.selectAll();
	}
	
	/**
	 * 新增商品
	 * @param good
	 */
	public void insert(Good good) {
		dao.save(good);
	}
	
	/**
	 * 查询所有
	 * @return
	 */
	public List<Good> findAll() {
		return dao.findAll();
	}
	
	/**
	 * 带条件的分页查询
	 * @param pageNum
	 * @param pageSize
	 * @param input
	 * @param selects
	 * @return
	 */
	public List<JSONObject> selectBySel(int pageNum, int pageSize, String cat_name, String input,
										String selects ) {
		String status = null;
		PageHelper.startPage(pageNum, pageSize);
		if (selects.length()>0 && selects.startsWith("50")) {
			status = selects;
		}
		return mapper.selectByWhere(cat_name, input, status);
	}
	
	/**
	 * 根据id查询
	 * @param goo_id
	 * @return
	 */
	public JSONObject findById(String goo_id) {
		return mapper.findByGid(goo_id);
	}
	
	
	
	/**
	 * 根据id查询
	 * @param goo_id
	 * @return
	 */
	public JSONObject findByGoodsId(int goo_id) {
		return mapper.findByGoodsId(goo_id);
	}
	

	public com.study.model.vo.Good findByGoodsIdByVo(int goo_id) {
		return mapper.findByGoodsIdByvo(goo_id);
	}
	
	/**
	 * 删除记录
	 * @param goo_id
	 */
	public void delete(int gooId) {
		Good good = dao.findByGooId(gooId);
		good.setGdel(0);
	}
	
	/**
	 * 编辑产品
	 * @param gname
	 * @param catid
	 * @param gid
	 * @param gstatus
	 * @param gspec
	 * @param gunit
	 * @param gprice
	 * @param gimg
	 * @param id
	 * @return
	 */
	public boolean editgood(String gname,int[] catid,Integer gid,Integer gstatus,
			Integer gspec,Integer gunit,BigDecimal gprice,String gimg,Integer ddtime,
			   String id) {
		Good good = dao.findByGooId(Integer.parseInt(id));
		good.setGooName(gname);
		good.setCategory(categoryDAO.findByCatId(catid[catid.length-1]));
		good.setDictionary1(disDao.findByDicId(gunit));
		good.setDictionary2(disDao.findByDicId(gstatus));
		good.setGooSpec(gspec);
		good.setGooPrice(gprice);
		//good.setGooImg(gimg);
		good.setDdtime(ddtime);
		return true;
	}
	
	/**
	 * 添加商品图片
	 * @param gid
	 * @param gimg
	 * @return
	 */
	public boolean findGood(Integer gid,String gimg) {
		Good good = dao.findByGooId(gid);
		good.setGooImg(gimg);
		return true;
	}
}
